package tetris.features;

import java.util.ArrayList;

import tetris.simulator.State;

public class CumulativeWellsFeature extends Feature {

  @SuppressWarnings("static-access")
  public ArrayList<Double> getFeatureVector(State tetrisState) {
    ArrayList<Double> featureVector = new ArrayList<Double>();
    int cumulativeWells = 0;
    for (int i = 0; i < tetrisState.COLS; i++) {
      for (int j = tetrisState.getTop()[i] - 1; j >= 0; j--) {
        boolean leftCell, thisCell, rightCell;
        // get left cell value (take into account boundary cases for left edge)
        if (i==0) {
          leftCell = true;
        } else {
          leftCell = tetrisState.getField()[j][i-1] > 0;
        }

        if (i== (tetrisState.COLS - 1)) {
          rightCell = true;
        } else {
          rightCell = tetrisState.getField()[j][i+1] > 0;
        }
        
        thisCell = tetrisState.getField()[j][i] > 0;

        if (leftCell && rightCell && !thisCell) {
          cumulativeWells++;
          // iterate for remaining well depth
          for (int k = j - 1; k >= 0; k--) {
            if (tetrisState.getField()[k][i] == 0)
              cumulativeWells++;
            else
              break;
          }
        }
      }
    }
    featureVector.add(new Double(cumulativeWells));
    return featureVector;
  }

}
